﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SourceGrid;
using Aulac.Rsc.Bss.Tags;
using Aulac.Global.Common;
using Aulac.Rsc.Bss;
using Aulac.Client.Data.Bss.Helper.DataItems;
using Aulac.Client.Data.Bss;

namespace Aulac.Client.Controls.Bss
{
    [System.ComponentModel.ToolboxItem(true)]
    public partial class GridBssDepr : Aulac.Client.Controls.UserControls.GridEditorCtrl
    {
        
        private Aulac.UtilityTools.DataGridEditors.CellEditors.ComboBox cbCapAssEditor = null;
        private Aulac.UtilityTools.DataGridEditors.CellEditors.ComboBox cbDeptEditor = null;
       
        public GridBssDepr()
        {
            InitializeComponent();
            grdData.DataSource = new Aulac.Client.Data.Bss.DataSet.DsGridBss.TableLineDepreciationDataTable();
            InitColumnShow();
        }

        #region Properties
        private InfoGrid.GridTableDepreciation _ColumnsGrd = new InfoGrid.GridTableDepreciation();
        public InfoGrid.GridTableDepreciation ColumnsGrd
        {
            get { return _ColumnsGrd; }
        }
        
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        
        #endregion 

        #region OverrideMethod
        protected override void OnFormLoad()
        {
            base.OnFormLoad();            
            ReLoadDataToControl();
        }
        public override void LoadDataToControl()
        {
            
            if (cbDeptEditor.Control.IsEmptyDataInList)
            {
                DataTable tbDept = cbDeptEditor.Control.GetTable();
                DataMdnItemsHelper.LoadDept(ref tbDept);
                cbDeptEditor.Control.DataSource = tbDept.DefaultView;
            }
             
            if (cbCapAssEditor.Control.IsEmptyDataInList )
            {
                DataTable tbCapAss = cbCapAssEditor.Control.GetTable();
                DataMdnItemsHelper.LoadCapAss(ref tbCapAss);
                cbCapAssEditor.Control.DataSource = tbCapAss.DefaultView;
            }
        }
        public override void ReLoadDataToControl()
        {
            cbDeptEditor.Control.ClearDataContainer();
            cbCapAssEditor.Control.ClearDataContainer();
            LoadDataToControl();
        }
        protected override void OnClearValueControls()
        {
            base.OnClearValueControls();
            this.grdData.ClearDataContainer();
            //this.txtTT.ClearValueControls();
        }

        protected override void OnCellEditorStarting(Aulac.UtilityTools.DataGridEditors.CellEditors.AulacEditorControl sender, CancelEventArgs e)
        {
            if (grdData.ReadOnly)
                return;
            if (Aulac.Global.Common.ValueHelper.EqualsValue(sender.Control, cbCapAssEditor))
            {
                string sCapAssID = grdData.GetValueStringDataCell(sender.EditCellContext.Position.Row, ColumnsGrd.CapAssID);
                if (!Aulac.Global.Common.ValueHelper.isEmpty(sCapAssID))
                {
                    DataView view = ((DataView)cbCapAssEditor.Control.DataSource);
                    view.RowFilter = string.Format("{0} = '{1}'", ColumnsGrd.CapAssID, sCapAssID);
                    cbCapAssEditor.Control.DataSource = view;
                }
            }
            if (Aulac.Global.Common.ValueHelper.EqualsValue(sender.Control, cbDeptEditor))
            {
                string sDeptID = grdData.GetValueStringDataCell(sender.EditCellContext.Position.Row, ColumnsGrd.DeptID);
                if (!Aulac.Global.Common.ValueHelper.isEmpty(sDeptID))
                {
                    DataView view = ((DataView)cbDeptEditor.Control.DataSource);
                    view.RowFilter = string.Format("{0} = '{1}'", ColumnsGrd.DeptID, sDeptID);
                    cbDeptEditor.Control.DataSource = view;
                }
            }
        }
        protected override void OnCellEditorEndding(Aulac.UtilityTools.DataGridEditors.CellEditors.AulacEditorControl sender, Aulac.UtilityTools.DataGridEditors.CellEditors.CellEditorEnddingEventArgs e)
        {
            if (e.Cancel == true)
                return;
            
            if (Aulac.Global.Common.ValueHelper.EqualsValue(sender.EditCellContext.Cell.Editor, cbCapAssEditor))
            {
                string sCapAssPrkID = (string)ValueHelper.ChangeType(cbCapAssEditor.Control.GetValue(cbCapAssEditor.Control.SelectedIndex, tagItems.CapAss.CapAssPrkID), typeof(string));
                //string sItemID = cbCapAssEditor.Control.GetValueItem(ColumnsGrd.ItemID);
                int pRows = checkCapAss(sCapAssPrkID);
                
                if (pRows != -1)
                {
                    Aulac.UtilityTools.MsgBox.Show(string.Format("Tài sản [{0}] đã được xét tính khấu hao ở dòng {1}!", cbCapAssEditor.Control.GetValueItem(ColumnsGrd.CapAssPrkID), pRows), "Warring", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    grdData.EndEditingRow(true);
                    e.Cancel = true;
                }
                else
                {
                    grdData.SetValueCell(grdData.SelectedIndexGridRow, tagItems.CapAss.CapAssPrkID, sCapAssPrkID);
                    grdData.SetValueCell(grdData.SelectedIndexGridRow, ColumnsGrd.CapAssID, cbCapAssEditor.Control.GetValueItem(ColumnsGrd.CapAssID));
                    grdData.SetValueCell(grdData.SelectedIndexGridRow, ColumnsGrd.CapAssName, cbCapAssEditor.Control.GetValueItem(ColumnsGrd.CapAssName));
                    
                    string sDeptPrkID = cbDeptEditor.Control.GetValueItem(tagItems.Department.DeptPrkID);
                    grdData.SetValueCell(grdData.SelectedIndexGridRow, tagItems.Department.DeptPrkID, sDeptPrkID);
                    grdData.SetValueCell(grdData.SelectedIndexGridRow, ColumnsGrd.DeptID, cbDeptEditor.Control.GetValueItem(ColumnsGrd.DeptID));
                }
                //grdData.SetValueCell(grdData.SelectedIndexGridRow, tagVch.Inv.OverInstock, false);
            }
            grdData.AutoStretchColumnsToFitWidth = true;
        }
        protected override void OnCellEditorEnd(Aulac.UtilityTools.DataGridEditors.CellEditors.AulacEditorControl sender, EventArgs e)
        {
            int count = grdData.ReadOnly == true ? grdData.Rows.Count : grdData.Rows.Count - 1;
            for (int i = 1; i < count; i++)
            {
                CalcAmtItemLine(i);
            }
            
            grdData.Refresh();
        }
        #endregion
       
        #region Private Method
        private int checkCapAss(string pCapAssPrkID)
        {
            for (int i = 1; i < grdData.Rows.Count - 2; i++)
            {
                object obj = grdData.GetValueDataCell(i, tagItems.CapAss.CapAssPrkID);
                string pValue = Convert.ToString(obj);
                if (ValueHelper.EqualsValue(pValue, pCapAssPrkID) && (i != grdData.SelectedIndexGridRow))
                    return i;
            }
            return -1;
        }

        private void InitColumnShow()
        {
            
            grdData.ClearColumsShow();

            cbCapAssEditor = new Aulac.UtilityTools.DataGridEditors.CellEditors.ComboBox(typeof(string));
            cbCapAssEditor.Control.DisplayMember = ColumnsGrd.CapAssID;
            cbCapAssEditor.Control.ColumnNames = string.Format("{0}, {1}", tagItems.CapAss.CapAssID, tagItems.CapAss.CapAssName);
            cbCapAssEditor.Control.ValueMember = ColumnsGrd.CapAssPrkID;
            cbCapAssEditor.Control.FieldMember = tagItems.CapAss.CapAssID;
            cbCapAssEditor.Control.DataSource = new Aulac.Client.Data.Bss.DataSet.Catalog.Dm_CapAssDataTable();
            cbCapAssEditor.Control.RemoveFieldMappingAll();
            cbCapAssEditor.Control.ListFieldMember = string.Format("{0}, {1}, {2}",tagItems.CapAss.CapAssPrkID, tagItems.CapAss.CapAssID, tagItems.CapAss.CapAssName);
            cbCapAssEditor.Control.DropDownWidth = 160;
            cbCapAssEditor.Control.ColumnWidths = "70,200";
            cbCapAssEditor.Control.TabKey = Aulac.UtilityTools.KeysUse.EnterForTab;
            DataGridColumn col = AddColumn(ColumnsGrd.CapAssID, "Mã tài sản", cbCapAssEditor);
            col.AutoSizeMode = SourceGrid.AutoSizeMode.EnableAutoSize;
            col.Width = 100;


            col = AddColumn(ColumnsGrd.CapAssName, "Tên tài sản", typeof(string));
            col.Width = 300;
            col.DataCell.Editor.EnableEdit = false;
            col.AutoSizeMode = SourceGrid.AutoSizeMode.Default;


            cbDeptEditor = new Aulac.UtilityTools.DataGridEditors.CellEditors.ComboBox(typeof(string));
            cbDeptEditor.Control.DisplayMember = ColumnsGrd.DeptID;
            cbDeptEditor.Control.ColumnNames = string.Format("{0}, {1}", tagItems.Department.DeptID, tagItems.Department.DeptName);
            cbDeptEditor.Control.ValueMember = ColumnsGrd.DeptID;
            cbDeptEditor.Control.FieldMember = ColumnsGrd.DeptID;
            cbDeptEditor.Control.DataSource = new Aulac.Client.Data.Bss.DataSet.Catalog.Dm_DepartmentDataTable();
            cbDeptEditor.Control.DropDownWidth = 270;
            cbDeptEditor.Control.ColumnWidths = "70,200";
            col = AddColumn(ColumnsGrd.DeptID, "Phòng ban", cbDeptEditor);
            col.AutoSizeMode = SourceGrid.AutoSizeMode.EnableAutoSize;
            col.Width = 100;
            col.DataCell.Editor = cbDeptEditor;

            // Tạm thời nguyên giá nhập vào, sau đưa từ bảng Dm_CapAss (từ phiếu ghi tăng)
            col = AddColumn(ColumnsGrd.OriginalPrice, "Nguyên giá", typeof(double));
            col.Width = 180;
            col.AutoSizeMode = SourceGrid.AutoSizeMode.EnableAutoSizeView;

            col = AddColumn(ColumnsGrd.DeprValue, "Giá trị tính khấu hao", typeof(double));
            col.Width = 160;
            col.AutoSizeMode = SourceGrid.AutoSizeMode.EnableAutoSizeView;
            col.DataCell.Editor.EnableEdit = false;

            col = AddColumn(ColumnsGrd.DeprRateMonth, "Tỉ lệ khấu hao tháng", typeof(string));
            col.Width = 180;
            col.AutoSizeMode = SourceGrid.AutoSizeMode.EnableAutoSizeView;
            col.DataCell.Editor.EnableEdit = false;

            col = AddColumn(ColumnsGrd.DeprValueMonth, "Giá trị khấu hao tháng", typeof(double));
            col.Width = 180;
            col.AutoSizeMode = SourceGrid.AutoSizeMode.EnableAutoSizeView;
            col.DataCell.Editor.EnableEdit = false;

            grdData.AutoStretchColumnsToFitWidth = true;
            grdData.ListFieldMember =
                StringHelper.ConvertArrayToString(", ", ColumnsGrd.CapAssID, ColumnsGrd.CapAssName, ColumnsGrd.CapAssPrkID, ColumnsGrd.DeprRateMonth, ColumnsGrd.DeprValue, ColumnsGrd.DeprValueMonth, ColumnsGrd.DeptID, ColumnsGrd.DeptPrkID, ColumnsGrd.ItemLine, ColumnsGrd.OriginalPrice, tagTab.TabLineDepr.TabDeprPrkID);
        }
	    #endregion
        
        #region Public Method
        public void CalcAmtItemLine(int pRow)
        {

            double dOrgPrice = grdData.GetValueDoubleDataCell(pRow, ColumnsGrd.OriginalPrice);
            double dDeprValue = grdData.GetValueDoubleDataCell(pRow, ColumnsGrd.DeprValue);
            double dDeprRateMonth = grdData.GetValueDoubleDataCell(pRow, ColumnsGrd.DeprRateMonth);
            double dDeprValueMonth = grdData.GetValueDoubleDataCell(pRow, ColumnsGrd.DeprValueMonth);
            int dSrvLife = 8; // Tạm thời lấy thời gian sử dụng = 8, sau đưa vào từ Dm_CapAss

            dDeprValue = dOrgPrice / dSrvLife;
            dDeprValueMonth = dDeprValue / 12;
            dDeprRateMonth = dDeprValueMonth / dOrgPrice;

            grdData.SetValueCell(pRow, ColumnsGrd.OriginalPrice, dOrgPrice);
            grdData.SetValueCell(pRow, ColumnsGrd.DeprValue, dDeprValue);
            grdData.SetValueCell(pRow, ColumnsGrd.DeprRateMonth, dDeprRateMonth);
            grdData.SetValueCell(pRow, ColumnsGrd.DeprValueMonth, dDeprValueMonth);
            
        }
        
        #endregion
    }
}
